<?php
	/*判断图片是否上传成功*/
	if ($_FILES['w_pic']['error']) {
		switch ($_FILES['w_pic']['error']) {
			case 1:
				$error = 1;
			break;
			case 2:
				$error = 2;
			break;
			case 3:
				$error = 3;
			break;
			case 4:
				$error = 4;
			break;
		}
	}
	else {
		/*获取文件类型*/
		list($maintype,$subtype) = explode("/", $_FILES['w_pic']['type']);
	
		/*如果主类型不是image，则推出*/
		if ($maintype != "image") {
			$error = 5;
		}
		else {
		/*重新定义上传后的位置和新文件名*/
		$upfile = "upload_wpic/".time().$_FILES['w_pic']['name'];
		
			/*判断是否为上传文件*/
			if (is_uploaded_file($_FILES['w_pic']['tmp_name'])) {
				if (!move_uploaded_file($_FILES['w_pic']['tmp_name'], iconv('UTF-8', 'gb2312', $upfile) )) {
				}
				//else setcookie("upload_file",$upfile);	
			}
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
</head>
<body>
<div id="pic_div">
<?php 
if ($error > 0) {
	switch ($error) {
			case 1:
				echo "上传图片不能超过3M，请重新上传！";
			break;
			case 2:
				echo "上传图片不能超过3M，请重新上传！";
			break;
			case 3:
				echo "图片损坏，请重新上传！";
			break;
			case 4:
				echo "未上传任何文件，请重新上传！";
			break;
			case 5:
				echo "非图片格式，请重新上传！";
			break;
		}
}
else {
?>
<span><?php echo $upfile; ?></span>
<img  src="<?php echo $upfile; ?>" height="200"/>
<?php } ?></div>
<input type="button" name="reload" id="reload" value="重新选择" />
</body>
<script type="text/javascript">
$(function(){
	var upfile = $("span").text();
	$.cookie('upload_file', upfile);
	var wpic = $("#pic_div span").text();
	$("#reload").click(function(){
		var go = "upload_wpic.php?a=del&k=" + wpic;
		window.location.href = go;
	});
});
</script>
</html>